﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HKQuery.Common
{
    public class OperateSession
    {
        /// <summary>
        /// 添加session(string类型),有效时间默认20分钟
        /// </summary>
        /// <param name="StrSessionName">session名称</param>
        /// <param name="StrSessionValue">session值</param>
        public static void add(string strSessionName, string strSessionValue)
        {
            HttpContext.Current.Session[strSessionName] = strSessionValue;
            HttpContext.Current.Session.Timeout = 60 * 10;
           
        }


        /// <summary>
        /// 添加session(string类型),并设置session有效时间
        /// </summary>
        /// <param name="StrSessionName">session名称</param>
        /// <param name="StrSessionValue">session值</param>
        /// <param name="timeOut">session有效时间</param>
        public static void add(string strSessionName, string strSessionValue, int timeOut)
        {
            HttpContext.Current.Session[strSessionName] = strSessionValue;
            HttpContext.Current.Session.Timeout = timeOut;
        }

        /// <summary>
        /// 添加session(List类型),有效时间默认20分钟
        /// </summary>
        /// <typeparam name="T">list的类型</typeparam>
        /// <param name="StrSessionName">session名称</param>
        /// <param name="StrSessionValue">session值</param>
        public static void addList<T>(string strSessionName, List<T> strSessionValue) where T : class
        {
            HttpContext.Current.Session[strSessionName] = strSessionValue;
            HttpContext.Current.Session.Timeout = 60 * 10;
        }


        /// <summary>
        /// 添加session(List类型),并设置session有效时间
        /// </summary>
        /// <typeparam name="T">list的类型</typeparam>
        /// <param name="StrSessionName">session名称</param>
        /// <param name="StrSessionValue">session值</param>
        /// <param name="timeOut">session有效时间</param>
        public static void addList<T>(string strSessionName, List<T> strSessionValue, int timeOut) where T : class
        {
            HttpContext.Current.Session[strSessionName] = strSessionValue;
            HttpContext.Current.Session.Timeout = timeOut;
        }


        /// <summary>
        /// 获取string类型的session，无则返回null值
        /// </summary>
        /// <param name="strSessionName">session名称</param>
        /// <returns></returns>
        public static string get(string strSessionName)
        {
            if (HttpContext.Current.Session[strSessionName] == null)
            {
                return null;
            }
            else
            {
                return HttpContext.Current.Session[strSessionName].ToString();
            }
        }

        /// <summary>
        /// 获取List类型的session，无则返回null值
        /// </summary>
        /// <typeparam name="T">list的类型</typeparam>
        /// <param name="strSessionName">session名称</param>
        /// <returns></returns>
        public static List<T> getList<T>(string strSessionName) where T : class
        {
            if (HttpContext.Current.Session[strSessionName] == null)
            {
                return null;
            }
            else
            {
                return (List<T>)HttpContext.Current.Session[strSessionName];
            }
        }


        /// <summary>
        /// 删除session
        /// </summary>
        /// <param name="strSessionNmae">session名称</param>
        public static void del(string strSessionNmae)
        {
            HttpContext.Current.Session[strSessionNmae] = null;
        }
    }
}